<template>
  <div class="shoplist-container">
    <ul>
      <li class="shoplist-item" v-for="(item,index) in recommendList" :key="index">
        <img :src="item.hd_thumb_url" alt width="100%" />
        <h4>{{item.goods_name}}</h4>
        <div class="item-info">
          <span class="info-price">￥39.9</span>
          <span class="info-count">{{item.sales_tip}}</span>
          <span class="info-serach" v-if="recommend">找相似</span>
          <span class="info-user" v-else>
            <img src="./images/user1.jpg" alt />
            <img src="./images/user2.jpg" alt />
          </span>
        </div>
      </li>
    </ul>
  </div>
</template>
<script>
import BSroll from "better-scroll";
import { mapState } from "vuex";
import { Indicator } from "mint-ui";
export default {
  data() {
    return {
      recommend: true,
      pagesize: 1,
      count: 10
    };
  },
  created() {
    Indicator.open("加载中...");
    setTimeout(() => {
      this.$store.dispatch("getRecommendList", {
        pagesize: this.pagesize,
        count: this.count,
        callback: () => {
          Indicator.close();
        }
      });
    }, 400);
  },
  computed: {
    ...mapState(["recommendList"])
  },
  watch: {
    // 有了数据过后我们，初始化BScroll
    recommendList() {
      //DOM刷新后再操作
      this.$nextTick(() => {
        this.pagesize++;
        this._initBScroll();
      });
    }
  },
  methods: {
    _initBScroll() {
      this.list = new BSroll(".shoplist-container", {
        click: true,
        probeType: 3
      });
      this.list.on("touchEnd", pos => {
        //下拉刷新，下拉到50执行刷新操作
        if (pos.y > 50) {
          console.log("下拉刷新");
        } else if (this.list.maxScrollY > pos.y + 20) {
          Indicator.open("加载中...");
          setTimeout(() => {
            this.$store.dispatch("getRecommendList", {
              pagesize: this.pagesize,
              count: this.count,
              callback: () => {
                Indicator.close();
              }
            });
          }, 400);
        }
      });
      //当页面的dom更新是，我们必须 监视刷新 bsroll的高度
      this.list.on("scrollEnd", () => {
        this.list.refresh();
      });
    }
  }
};
</script>
<style lang='scss' scoped>
.shoplist-container {
  height: 90vh;
  overflow: hidden;
  margin-top: 5px;
  border: 1px solid red;
}
ul {
  width: 100%;
  display: flex;
  flex-flow: wrap;
}
.shoplist-item {
  width: 49.5%;
  display: flex;
  flex-direction: column;
  align-items: center;
  margin-bottom: 10px;
  h4 {
    height: 20px;
    line-height: 20px;
    overflow: hidden;
    color: #666;
    margin: 5px;
    font-size: 15px;
  }
  .item-info {
    display: flex;
    justify-content: center;
    align-items: center;
    width: 100%;
    margin-bottom: 8px;
    .info-price {
      flex: 1;
      color: orangered;
      font-size: 16px;
    }
    .info-count {
      flex: 3;
      color: #666;
      font-size: 10px;
      text-align: center;
    }
    .info-serach {
      flex: 2;
      font-size: 12px;
      color: orangered;
      border: 1px solid orangered;
      text-align: center;
      padding: 2px;
      border-radius: 5px;
    }
    .info-user {
      flex: 2;
      img {
        width: 25px;
        border-radius: 50%;
      }
      img:nth-child(2) {
        margin-left: -10px;
      }
    }
  }
}
.shoplist-item:nth-child(even) {
  margin-left: 1%;
}
</style>